package com.guigui.gulimall.order.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.guigui.common.to.MemberDto;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class GulimallAuthInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberDto> memberThreadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        JSONObject user = (JSONObject) session.getAttribute("user");
        if (user == null) {
            response.sendRedirect("http://auth.gulimall.com");
            return false;
        }
        memberThreadLocal.set(user.toJavaObject(MemberDto.class));
        return true;
    }

}
